//Declare out the name of the test module to make dojo's module loader happy.
dojo.provide("dojox.data.tests.AndOrWriteStore");
dojo.require("dojox.data.AndOrWriteStore");

doh.register("dojox.data.tests.AndOrWriteStore", [
    function simpleSyncTest(doh) {
        doh.assertTrue(true);
        doh.assertTrue(1);
        doh.assertTrue(!false);
    },
    function simpleAsyncTest(doh) {
        var deferred = new doh.Deferred();
        setTimeout(function() {
            try {
                doh.assertTrue(true);
                deferred.callback(true);
            } catch (e) {
                deferred.errback(e);
            }
        }, 500);
        return deferred;
    },
    {
        name: "myTestFixture",
        setUp: function() {
            // Setup to do before runTest.
            this.date = new Date();
        },
        runTest: function(doh) {
            // Our test function to run.
            //doh.assertEqual("blah", this.date.blahProp);
            //doh.assertFalse(this.thingerToTest.falseProp);
            // ...
        },
        tearDown: function() {
            // cleanup to do after runTest.
        },
        timeout: 3000 // 3 second timeout.
    },
     function testWriteAPI_modify_delete_revert(doh) {
        // Our test function to run.
        
        //  summary: 
        //      Test of a new item, modify it, delete it, then revert, to ensure the state remains consistent.  Added due to #9022.
        //  description:
        //      Test of a new item, modify it, delete it, then revert, to ensure the state remains consistent.  Added due to #9022.
        var data = {
            url: dojo.moduleUrl("tests", "data/reference_integrity.json").toString()
        };
        var store = new dojox.data.AndOrWriteStore(data);
        var i;
        var found = false;
        
        var deferred = new doh.Deferred();
        doh.assertTrue(!store.isDirty());
        
        var onError = function(error, request) {
            deferred.errback(error);
        };
        
        var initialFetch = function(item) {
            var nameBefore = store.getValue(item, "name");
            console.debug("before", nameBefore);
            
            store.setValue(item, "name", "new name");
            var nameAfter = store.getValue(item, "name");
            console.debug("after", nameAfter);
            
            store.deleteItem(item);
            
            store.revert();
            
            var afterRevert = function(item) {
                var nameRevert = store.getValue(item, "name");
                console.debug("revert", nameRevert);
                
                doh.assertEqual(nameBefore, nameRevert);
                deferred.callback(true);
            };
            
            store.fetchItemByIdentity({
                identity: "10",
                onItem: afterRevert,
                onError: onError
            });
        };
        store.fetchItemByIdentity({
            identity: "10",
            onItem: initialFetch,
            onError: onError
        });
        return deferred; //Object
    },
    function testWriteAPI_modify_modify_revert() {
        // Our test function to run.
        
        //  summary: 
        //      Test of a new item, modify it, delete it, then revert, to ensure the state remains consistent.  Added due to #9022.
        //  description:
        //      Test of a new item, modify it, delete it, then revert, to ensure the state remains consistent.  Added due to #9022.
        var data = {
            url: dojo.moduleUrl("tests", "data/reference_integrity.json").toString()
        };
        var store = new dojox.data.AndOrWriteStore(data);
        var i;
        var found = false;
        
        var deferred = new doh.Deferred();
        doh.assertTrue(!store.isDirty());
        
        var onError = function(error, request) {
            deferred.errback(error);
        };
        
        var initialFetch = function(item) {
            var nameBefore = store.getValue(item, "name");
            console.debug("before", nameBefore);
            
            store.setValue(item, "name", "new name 1");
            var nameAfter = store.getValue(item, "name");
            console.debug("after 1", nameAfter);
            
            store.setValue(item, "name", "new name 2");
            var nameAfter = store.getValue(item, "name");
            console.debug("after 2", nameAfter);
            
            store.revert();
            
            var afterRevert = function(item) {
                var nameRevert = store.getValue(item, "name");
                console.debug("revert", nameRevert);
                
                doh.assertEqual(nameBefore, nameRevert);
                deferred.callback(true);
            };
            
            store.fetchItemByIdentity({
                identity: "10",
                onItem: afterRevert,
                onError: onError
            });
        };
        store.fetchItemByIdentity({
            identity: "10",
            onItem: initialFetch,
            onError: onError
        });
        return deferred; //Object
    }
]);
